package com.tgpms.web.assets.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tgpms.web.assets.dao.FaRepairMapper;
import com.tgpms.web.assets.entity.FaRepair;
import com.tgpms.web.assets.entity.FaRepairKey;
import com.tgpms.web.assets.service.FaRepairService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.interfaces.dto.VformInterface;
import com.tgpms.util.Common;
import com.tgpms.util.PageInfo;
import com.tgpms.util.PageView;
import com.tgpms.util.VformBeanUtil;
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
public class FaRepairServiceImpl implements FaRepairService {

	@Autowired
	private VformInterface vformInterface;
	
	/**
	* @Author newway
	* @Description 查询资产大修集合
	* @Date 2019年8月31日下午3:27:47
	* @Param 
	* @return 
	**/
	@Override
	public Result selFaRepairList(PageInfo pageInfo) {
		JSONObject param = new JSONObject();
		Object selparam = pageInfo.getQueryMap().get("selparam");
		Object leftdition = pageInfo.getQueryMap().get("leftdition");
		Object condition = pageInfo.getQueryMap().get("condition");
		Object dataowner = pageInfo.getQueryMap().get("dataowner");
		if(Common.isEmpty(selparam)) {
			param.put("[SQL1]", " ");
		}else {
			param.put("[SQL1]", selparam.toString());
		}
		if(Common.isEmpty(leftdition)) {
			param.put("[SQL2]", " ");
		}else {
			param.put("[SQL2]", leftdition.toString());
		}
		if(Common.isEmpty(condition)) {
			param.put("[SQL3]", " ");
		}else {
			param.put("[SQL3]", condition.toString());
		}
		Result result = vformInterface.multiForMsQueryBySql("SCWZC013", param.toJSONString(), pageInfo.getPage(), 
				pageInfo.getSize(), "Y", "Y", dataowner.toString());
		return result;
	}

	/**
	 * @Author newway
	 * @Description 添加固定资产大修改造信息
	 * @Date 2019年8月12日上午10:58:41
	 * @Param com.tgpms.web.assets.entity.FaRepair
	 * @return Integer
	 **/
	@SuppressWarnings("unchecked")
	@Override
	public Map<String,String> addFaRepairInfo(FaRepair faRepair) {
		Map<String,String> map = new HashMap<String, String>();
		String faRepairNo = "";
		String workNo = "";
		JSONObject param = new JSONObject();
		param.put("[SQL1]", " ");
		param.put("[SQL2]", " ");
		param.put("[SQL3]", " and t.FIXED_ASSET_NO='"+faRepair.getFixedAssetNo()
		+"' order by t.FA_REPAIR_NO desc");
		Result result = vformInterface.multiForMsQueryBySql("SCWZC013", param.toJSONString(), "1", 
				"1000", "N", null, null);
		if(result==null || !result.getSuccess()) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
			return map;
		}
		String resData = result.getData().toString();
		JSONObject resJson = JSONObject.parseObject(resData);
		String resList = resJson.getString("rows");
		if(resData.equals("{\"message\":\"login\"}")) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
			return map;
		}
		if(resData.equals("{\"total\":\"0\",\"rows\":[]}")) {
			faRepairNo = "000001";
			workNo = "000001";
		}else {
			List<Map<String,String>> list = JSONArray.parseObject(resList, List.class);
			String fixno = list.get(0).get("FAREPAIRNO");
			String max = (Integer.parseInt(fixno)+1)+"";
			faRepairNo = String.format("%06d",Integer.parseInt(max));
			workNo = faRepairNo;
		}
		System.out.println("得到主键为:  "+faRepairNo);
		faRepair.setFaRepairNo(faRepairNo);
		faRepair.setWorkNo(workNo);
		
		//对象转json字符串, 字段驼峰转下划线大写字段
		JSONObject json = VformBeanUtil.entityToJsonString(faRepair);
		if(json==null || json.isEmpty()) {
			map.put("code", "-2");
			map.put("info", ExceptionExplain.ERROR_BY_PARSING.getExplain());
			return map;
		}
		//补上主键
		json.put("FA_REPAIR_NO", faRepair.getWorkNo());
		json.put("FIXED_ASSET_NO", faRepair.getFixedAssetNo());
		json.put("WORK_NO", faRepair.getWorkNo());
		System.out.println("vform增加上送参数   "+json.toString());
		//vform同步插入
		result = vformInterface.insert("D8002", json);
		System.out.println("vform增加返回结果   "+result);
		if(result==null || !result.getSuccess()) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
			return map;
		}
		resData = result.getData().toString();
		if(resData.equals("{\"message\":\"login\"}")) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
			return map;
		}
		JSONObject reJson = JSONObject.parseObject(resData);
		if(reJson.get("status").toString().equals("error")) {
			map.put("code", "-4");
			map.put("info", reJson.get("validateError").toString());
			return map;
		}else {
			map.put("code", "1");
			map.put("info", ExceptionExplain.SUCCESS_BY_INSERT.getExplain());
			map.put("data", faRepair.getFaRepairNo());
		}
		return map;
	}

	/**
	 * @Author newway
	 * @Description 更新固定资产大修改造信息
	 * @Date 2019年8月12日上午11:03:39
	 * @Param com.tgpms.web.assets.entity.FaRepair
	 * @return Integer 
	 **/
	@Override
	public Map<String,String> updateFaRepairInfo(FaRepair faRepair) {
		Map<String,String> map = new HashMap<String, String>();
		JSONObject keyjson = new JSONObject();
		keyjson.put("FA_REPAIR_NO", faRepair.getFaRepairNo());
		keyjson.put("FIXED_ASSET_NO", faRepair.getFixedAssetNo());
		keyjson.put("WORK_NO", faRepair.getWorkNo());
		JSONObject pajson = new JSONObject();
		pajson = VformBeanUtil.entityToJsonStringByUpdate(faRepair);
		pajson.remove("FA_REPAIR_NO");
		pajson.remove("FIXED_ASSET_NO");
		pajson.remove("WORK_NO");

		System.out.println("vform更新上送主键   "+keyjson.toString());
		System.out.println("vform更新上送参数   "+pajson.toString());
		//vform同步更新
		Result result = vformInterface.update("D8002", keyjson.toString(),pajson.toString(),"Y","Y",faRepair.getDataOwner());
		System.out.println("vform更新返回结果   "+result);
		if(result==null || !result.getSuccess()) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
			return map;
		}
		String resData = result.getData().toString();
		JSONObject reJson = JSONObject.parseObject(resData);
		if(reJson.get("status").toString().equals("error")) {
			map.put("code", "-4");
			map.put("info", reJson.get("validateError").toString());
			return map;
		}else {
			map.put("code", "1");
			map.put("info", ExceptionExplain.SUCCESS_BY_UPDATE.getExplain());
		}
		return map;
	}

	/**
	 * @Author newway
	 * @Description 删除固定资产大修改造信息
	 * @Date 2019年8月12日上午11:06:23
	 * @Param FaRepairNo 大修改造序号
	 * @Param fixedAssetNo 资产编码
	 * @Param workNo 工作编码 
	 * @return Integer
	 **/
	@Override
	public Map<String,String> deleteFaRepairInfo(String faRepairNo, String fixedAssetNo, String workNo) {
		Map<String,String> map = new HashMap<String, String>();
		JSONObject keyjson = new JSONObject();
		keyjson.put("FA_REPAIR_NO", faRepairNo);
		keyjson.put("FIXED_ASSET_NO", fixedAssetNo);
		keyjson.put("WORK_NO", workNo);
		System.out.println("vform删除上送主键   "+keyjson.toString());
		//vform同步删除
		Result result = vformInterface.delete("D8002", keyjson.toJSONString());
		System.out.println("vform删除返回结果   "+result);
		if(result==null || !result.getSuccess()) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
			return map;
		}
		String resData = result.getData().toString();
		JSONObject reJson = JSONObject.parseObject(resData);
		if(reJson.get("status").toString().equals("error")) {
			map.put("code", "-4");
			map.put("info", reJson.get("validateError").toString());
			return map;
		}else {
			map.put("code", "1");
			map.put("info", ExceptionExplain.SUCCESS_BY_DELETE.getExplain());
		}
		return map;
	}

	


	
	

	

}
